<?php

namespace app\store\controller;

use app\store\model\Store as StoreModel;
use think\Db;
/**
 * 后台首页
 * Class Index
 * @package app\store\controller
 */
class Index extends Controller
{
    /**
     * 后台首页
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        // 当前用户菜单url
        $menus = $this->menus();
        $url = current(array_values($menus))['index'];
        if ($url !== 'index/index') {
            $this->redirect($url);
        }
        $model = new StoreModel;
        $data  = $model->getHomeData();
        // fujuhao 重置当天的订单销售总额和支付订单，调用之前的会影响统计页面的数据
    	$data['widget-outline']['order_total_price']['tday'] = $this->getOrderTotalPrice(date('d'));
    	$data['widget-outline']['order_total_price']['ytd'] = $this->getOrderTotalPrice(date('d',strtotime('-1 day')));
    	
    	$data['widget-outline']['order_total']['tday'] = $this->getPayOrderTotal(date('d'));
    	$data['widget-outline']['order_total']['ytd'] = $this->getPayOrderTotal(date('d',strtotime('-1 day')));
    	
    	// 七天数据
    	// $data['widget-echarts']['date'] = [
    	// 		date("Y").'-'.date('m').'-'.date("d",strtotime('-6 day')),
    	// 		date("Y").'-'.date('m').'-'.date("d",strtotime('-5 day')),
    	// 		date("Y").'-'.date('m').'-'.date("d",strtotime('-4 day')),
    	// 		date("Y").'-'.date('m').'-'.date("d",strtotime('-3 day')),
    	// 		date("Y").'-'.date('m').'-'.date("d",strtotime('-2 day')),
    	// 		date("Y").'-'.date('m').'-'.date("d",strtotime('-1 day')),
    	// 		date("Y").'-'.date('m').'-'.date("d")
    	// 	];
    	
    	$data['widget-echarts']['order_total'] = json_encode([
    			sprintf('%.2f',$this->getPayOrderTotal(date('d',strtotime('-6 day')))),
    			sprintf('%.2f',$this->getPayOrderTotal(date('d',strtotime('-5 day')))),
    			sprintf('%.2f',$this->getPayOrderTotal(date('d',strtotime('-4 day')))),
    			sprintf('%.2f',$this->getPayOrderTotal(date('d',strtotime('-3 day')))),
    			sprintf('%.2f',$this->getPayOrderTotal(date('d',strtotime('-2 day')))),
    			sprintf('%.2f',$data['widget-outline']['order_total']['ytd']),
    			sprintf('%.2f',$data['widget-outline']['order_total']['tday'])
    		]);
    	// var_dump($data['widget-echarts']);return;
    	$data['widget-echarts']['order_total_price'] = json_encode([
    			sprintf('%.2f',$this->getOrderTotalPrice(date('d',strtotime('-6 day')))),
    			sprintf('%.2f',$this->getOrderTotalPrice(date('d',strtotime('-5 day')))),
    			sprintf('%.2f',$this->getOrderTotalPrice(date('d',strtotime('-4 day')))),
    			sprintf('%.2f',$this->getOrderTotalPrice(date('d',strtotime('-3 day')))),
    			sprintf('%.2f',$this->getOrderTotalPrice(date('d',strtotime('-2 day')))),
    			sprintf('%.2f',$data['widget-outline']['order_total_price']['ytd']),
    			sprintf('%.2f',$data['widget-outline']['order_total_price']['tday'])
    		]);

         
        // print_r($model->getHomeData());return;
        return $this->fetch('index', ['data' => $data]);
    }
    
    // 根据日期获取总销售额 fujuhao
	private function getOrderTotalPrice($day){
		// 当天0点时间戳和23:59:59的时间戳
		$year	= date("Y");
		$month	= date("m");
		// $day	= date("d");
		$start	= mktime(0,0,0,$month,$day,$year);//当天开始时间戳
		$end	= mktime(23,59,59,$month,$day,$year);//当天结束时间戳
		// 当天的销售总额
        return db('order')->where('pay_time','between time',[$start,$end])
        			->where('pay_status', '=', 20)
            		// ->where('order_status', '<>', 20) // 包含了取消的订单
            		->where('order_status', '<>', 21)
            		->where('is_delete', '=', 0)
            		->sum('pay_price');
	}
	
	// 根据日期获取总订单数
	private function getPayOrderTotal($day){
		// 当天0点时间戳和23:59:59的时间戳
		$year	= date("Y");
		$month	= date("m");
		// $day	= date("d");
		$start	= mktime(0,0,0,$month,$day,$year);//当天开始时间戳
		$end	= mktime(23,59,59,$month,$day,$year);//当天结束时间戳
		
		return db('order')->where('pay_time','between time',[$start,$end])
        			->where('pay_status', '=', 20)
            		// ->where('order_status', '<>', 20) // 包含了取消的订单
            		->where('order_status', '<>', 21)
            		->where('is_delete', '=', 0)
            		->count();
	}
}
